﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;
using TjZheng.SSO.Business;
using TjZheng.SSO.DataEntity;
using TjZheng.Cache;

namespace TjZheng.SSO.Process
{
    public partial class UserProcess
    {
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public virtual InvokeResult AddUserInfo(string userName, string password)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            BizUserInfo objBizUserInfo = new BizUserInfo();
            bool hasRegister = objBizUserInfo.AnyCache("", "UserName=@UserName", "UserName".CreateSqlParameters(userName));
            if (hasRegister)
            {
                objInvokeResult.ResultCode = "HasRegister";
                objInvokeResult.ResultMessage = "对不起，用户名：" + userName + "已经注册";
                return objInvokeResult;
            }
            UserInfo objUserInfo = new UserInfo();
            objUserInfo.UserName = userName;
            objUserInfo.Salt = Guid.NewGuid().ToString();
            objUserInfo.Password = objUserInfo.Password.EncodeMD5(objUserInfo.Salt);
            objInvokeResult.Data = objBizUserInfo.Add(objUserInfo);
            return objInvokeResult;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="rePassword"></param>
        /// <returns></returns>
        public virtual InvokeResult Register(string userName, string password, string rePassword)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (userName.IsNull())
            {
                objInvokeResult.ResultCode = "UserNameNull";
                objInvokeResult.ResultMessage = "对不起，用户名不能为空";
                return objInvokeResult;
            }
            if (password.IsNull())
            {
                objInvokeResult.ResultCode = "PasswordNull";
                objInvokeResult.ResultMessage = "对不起，用户密码不能为空";
                return objInvokeResult;
            }
            if (rePassword.IsNull())
            {
                objInvokeResult.ResultCode = "RePasswordNull";
                objInvokeResult.ResultMessage = "对不起，重复不能为空";
                return objInvokeResult;
            }
            if (!string.Equals(password, rePassword))
            {
                objInvokeResult.ResultCode = "NotMatchRePassword";
                objInvokeResult.ResultMessage = "对不起，两次输入的密码不一致";
                return objInvokeResult;
            }
            userName = userName.Trim();
            objInvokeResult = AddUserInfo(userName, password);
            if (objInvokeResult.ResultCode != "Success")
            {
                return objInvokeResult;
            }
            string userId = objInvokeResult.Data.ToString();
            BizTempInfo objBizTempInfo = new BizTempInfo();
            TempInfo objTempInfo = new TempInfo
            {
                ObjectCode = "UserInfo",
                ObjectId = userId,
                EffectiveDate = DateTime.Now.AddHours(2)
            };
            objBizTempInfo.Add(objTempInfo);

            objInvokeResult.ResultMessage = "注册成功";
            return objInvokeResult;
        }

    }
}
